# app/service/group_service.py
from db.redis_part import xibie_redis
from db.mongo_part import xibie_mongo
from db.mysql_part import get_mysql_session
from db.models import RaceGroupPetBanHide, RaceGroupPetTypeHide
from app.exception.cus_exception import InvalidActionException
import json
from sqlalchemy import select
from utils.log_util import logger


async def get_group_config(group_id):
    """获取比赛组配置

    Args:
        group_id (_type_): _description_

    Raises:
        InvalidActionException: _description_

    Returns:
        _type_: _description_
    """
    if xibie_redis.exists(f"xibie-group-{group_id}"):
        return json.loads(xibie_redis.get(f"xibie-group-{group_id}"))
    group_config = await xibie_mongo.race_groups.find_one({'group_id': group_id})
    del group_config['_id']
    if group_config is None:
        raise InvalidActionException(f"不存在的分组{group_id}")
    xibie_redis.set(f"xibie-group-{group_id}", json.dumps(group_config))
    xibie_redis.expire(f"xibie-group-{group_id}", 60 * 60 * 24)
    return group_config


async def get_group_pet_bans(group_id):
    """获取比赛组禁用的精灵

    Args:
        group_id (_type_): _description_

    Returns:
        _type_: _description_
    """
    if xibie_redis.exists(f"xibie-group-{group_id}-bans"):
        return [int(pet_id) for pet_id in xibie_redis.smembers(f"xibie-group-{group_id}-bans")]

    async with get_mysql_session() as session:
        stmt = select(RaceGroupPetBanHide).where(RaceGroupPetBanHide.group_id == group_id,
                                                 RaceGroupPetBanHide.is_banned == 1)
        res = await session.execute(stmt)
        group_bans = res.scalars().all()
        group_bans = [ban.pet_id for ban in group_bans]
        xibie_redis.sadd(f"xibie-group-{group_id}-bans", *group_bans)
        xibie_redis.expire(f"xibie-group-{group_id}-bans", 60 * 60 * 24)
        return group_bans


async def get_group_pet_unhide(group_id):
    """获取比赛组不能暗手的精灵

    Args:
        group_id (_type_): _description_

    Returns:
        _type_: _description_
    """
    if xibie_redis.exists(f"xibie-group-{group_id}-unhide"):
        return xibie_redis.smembers(f"xibie-group-{group_id}-unhide")

    async with get_mysql_session() as session:
        stmt = select(RaceGroupPetBanHide).where(RaceGroupPetBanHide.group_id == group_id,
                                                 RaceGroupPetBanHide.is_hide_enable == 0)
        res = await session.execute(stmt)
        group_unhide = res.scalars().all()
        group_unhide = [ban.pet_id for ban in group_unhide]
        xibie_redis.sadd(f"xibie-group-{group_id}-unhide", *group_unhide)
        xibie_redis.expire(f"xibie-group-{group_id}-unhide", 60 * 60 * 24)
        return group_unhide


async def get_group_pet_type_hide(group_id):
    """获取比赛组不能暗手的精灵属系

    Args:
        group_id (_type_): _description_

    Returns:
        _type_: _description_
    """
    if xibie_redis.exists(f"xibie-group-{group_id}-unhide-types"):
        return xibie_redis.smembers(f"xibie-group-{group_id}-unhide-types")
    async with get_mysql_session() as session:
        stmt = select(RaceGroupPetTypeHide).where(RaceGroupPetTypeHide.group_id == group_id,
                                                     RaceGroupPetTypeHide.is_hide_enable == 0)
        res = await session.execute(stmt)
        group_unhide = res.scalars().all()
        group_unhide = [ban.pet_type_id for ban in group_unhide]
        xibie_redis.sadd(f"xibie-group-{group_id}-unhide-types", *group_unhide)
        xibie_redis.expire(f"xibie-group-{group_id}-unhide-types", 60 * 60 * 24)
        return group_unhide